<script>
    // 账户模态表单页
    Vue.component('export-dialog', {
        template: `
      <el-dialog :visible.sync="dialogShow" width="600px" @close="closeModal">
         <h3 slot="title">
                <span >导入</span>
         </h3>

        <h5>1.请先下载模板,根据模板要求输入需要导入的数据</h5>
        <div style="margin-bottom:9px"></div>
        <a :href="downloadUrl" style="color:skyblue">下载导入模板</a>
        <div style="margin-bottom:15px"></div>
        <h5>2.选择文件</h5>
        <div style="margin-bottom:7px"></div>
        <div style="display:flex;align-items:center">
            <div style="cursor:pointer;width:100px;height:35px;background-color:rgba(52,128,223,0.9);border-radius:2px;text-align:center;line-height:35px;color:#FFF;font-size:12px" @click="upload()">选择文件</div>
            <div style="margin-left:10px;color:#999999;font-size:12px">(最多上传一个文件，最大10MB)</div>
        </div>


</el-dialog>
    `,
        props: ['dialog-show','download-url','upload-url','type'],
        computed: {
            currentTallyId() {
                return this.$store.state.currentTallyId
            },
            currentTally () {
                return this.$store.state.currentTally
            },
            accountTags() {
                return this.$store.state.accountTags
            }
        },
        watch: {
            dialogShow:{
                handler (newVal) {},
                deep:true,
                immediate: true
            }
        },
        data:function(){
            return {

            }
        },
        methods: {
            closeModal(){
                this.$emit('modalCloseq')
            },
            upload() {
                const _this = this;
                let that = this
                const fileType = ['xls','xlsx']
                const inputFile = document.createElement("input")
                inputFile.type = "file"
                inputFile.style.display = "none"
                document.body.appendChild(inputFile)
                inputFile.click()
                inputFile.addEventListener("change",function() {
                    const file = inputFile.files[0];
                    var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
                    if (!fileType.includes(testmsg)) {
                        that.$message({
                            type: 'error',
                            message: '上传的文件格式只能是,xls,xlsx'
                        })
                        document.body.removeChild(inputFile);
                        return false;
                    }
                    const formData = new FormData();
                    formData.append("file", file);
                    formData.append("tally_id", that.currentTallyId);

                    if(_this.type != undefined){
                        formData.append("type", _this.type);
                    }


                    axios.post(_this.uploadUrl,formData,{
                        headers: {'token': loginToken},
                    }).then(function(res) {
                        if(res.data.status === 'success'){
                            that.$message({
                                type: 'success',
                                message: '操作成功~'
                            })
                            _this.closeModal();
                        }else{
                            that.$message({
                                type: 'error',
                                message: '解析数据失败:'+res.data.error_message
                            })
                        }
                    })

                });
            }
        }
    })
</script>
<style>

</style>
